package com.mbigger.admin.dao;

import com.mbigger.admin.beans.PageQuery;
import com.mbigger.admin.entity.Acl;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

@Mapper
public interface AclMapper {
	
	@Insert("INSERT INTO sys_acl (code, name, acl_module_id, url, type, status, seq, remark, operator, operate_time, operate_ip) "
			+ "VALUES(#{code}, #{name}, #{aclModuleId}, #{url}, #{type}, #{status}, #{seq}, #{remark}, #{operator}, #{operateTime}, #{operateIp})")
	int insert(Acl acl);
	
	@Delete("DELETE FROM sys_acl WHERE id=#{id}")
	int deleteById(@Param("id")Integer id);
	
	@Update("UPDATE sys_acl SET code=#{code}, name=#{name}, acl_module_id=#{aclModuleId}, url=#{url}, type=#{type}, status=#{status}, seq=#{seq}, remark=#{remark}, operator=#{operator}, operate_time=#{operateTime}, operate_ip=#{operateIp}")
	int updateById(@Param("acl")Acl acl);
	
	@Select("SELECT * FROM sys_acl where id=#{id}")
	Acl selectById(@Param("id")Integer id);
	
	@Select("SELECT count(1) FROM sys_acl WHERE acl_module_id=#{aclModuleId}")
	int countByAclModuleId(@Param("aclModuleId")int aclModuleId);
	
	@Select("SELECT * FROM sys_acl WHERE acl_module_id=#{aclModuleId} ORDER BY seq ASC LIMIT #{page.offset}, #{page.pageSize}")
	List<Acl> getPageByAclModuleId(@Param("aclModuleId")int aclModuleId, @Param("page")PageQuery page);
	
	@Select("<script>"
			+ "SELECT count(1) FROM sys_acl WHERE acl_module_id=#{aclModuleId} "
			+ "<if test='id != null'>"
			+ "AND id!=#{id}"
			+ "</if>"
			+ "</script>")
	int countByNameAndAclModuleId(@Param("aclModuleId")int aclModuleId, @Param("name")String name, @Param("id")Integer id);
	
	@Select("SELECT * FROM sys_acl")
	List<Acl> getAll();
	
	@Select("<script>"
			+ "SELECT * FROM sys_acl WHERE id IN "
			+ "<foreach collection='idList' item='id' open='(' close=')' separator=','>"
			+ "#{id}"
			+ "</foreach>"
			+ "</script>")
	List<Acl> getByIdList(@Param("idList")List<Integer> idList);
	
	@Select("SELECT * FROM sys_acl WHERE permission=#{permission}")
	List<Acl> getByPermission(@Param("permission")String permission);
}